package man4j;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

public class MainScreen extends List implements CommandListener {	
	private Command exitCommand;
	
	private MainMIDlet mainMIDlet;
	
	public MainScreen(final MainMIDlet mainMIDlet) {
		super("Main", List.IMPLICIT);
		
		this.mainMIDlet = mainMIDlet;
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		addCommand(exitCommand);
		
		setCommandListener(this);
		
		try {
			int h = Display.getDisplay(mainMIDlet).getBestImageHeight(Display.LIST_ELEMENT);
			
			append(GoScreen.GO, ImageUtils.resizeImage(Image.createImage(getClass().getResourceAsStream("/play.png")), h, h));			
			append(CurrentTaskScreen.EDIT_CURRENT_TASKS, ImageUtils.resizeImage(Image.createImage(getClass().getResourceAsStream("/edit.png")), h, h));
			append(CommonTaskScreen.EDIT_COMMON_TASKS, ImageUtils.resizeImage(Image.createImage(getClass().getResourceAsStream("/star.png")), h, h));
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {
			mainMIDlet.notifyDestroyed();
        }
		
		if (c == List.SELECT_COMMAND) {
			int selected = getSelectedIndex();
			
			String itemName = getString(selected);
			
			if (itemName.equals(CommonTaskScreen.EDIT_COMMON_TASKS)) {
				Display.getDisplay(mainMIDlet).setCurrent(mainMIDlet.getCommonTasksScreen());				
			}
			
			if (itemName.equals(CurrentTaskScreen.EDIT_CURRENT_TASKS)) {
				Display.getDisplay(mainMIDlet).setCurrent(mainMIDlet.getCurrentTasksScreen());				
			}
			
			if (itemName.equals(GoScreen.GO)) {
				mainMIDlet.getGoScreen().update();
				
				Display.getDisplay(mainMIDlet).setCurrent(mainMIDlet.getGoScreen());				
			}
		}
	}
}
